﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.KnowingAgreeBook
{
    public partial class AgreeBookNursingHistory : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo Patientinfo;
        string FormName;
        string FormId;
        #endregion

        #region 窗体构造函数
        public AgreeBookNursingHistory(Tb_User loginUser, PatientInfo patientInfo, string FormName, string FormID)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.FormName = FormName;
            this.FormId = FormID;
            LoadData("", "");
            SetBandGridView();            
            dateStart.DateTime = DateTime.Now.AddDays(-30);            
            dateEnd.DateTime = DateTime.Now;
            YY_QiTa.Width = 30;
        }
        #endregion

        #region LoadData显示
        private void LoadData(string StatDate, string EndDate)
        {
            string patientid = this.Patientinfo.PATIENT_ID;//住院号
            int VisitId = this.Patientinfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.Knowing;//10002  //表单ID
            string select_sql = @"SELECT  * ,
                                    '' AS ZaoDong ,
                                    '' AS ShenZhiYiChang ,                                    
                                    '' AS JingShenZhangAi ,
                                    '' AS ZhiTiZhiDong ,
                                    '' AS YuanYinQiTa,
                                    
                                    '' AS BaGuan,
                                    '' AS ZiShang,
                                    '' AS ShangRen,
                                    '' AS ZhuiChuang,
                                    '' AS ZhiTiGuoDuHuoDong,
                                    '' AS YuFangQiTa ,

                                    '' AS PiFuHongZhong ,
                                    '' AS PoKui,
                                    '' AS QiPao,
                                    '' AS GanRan,
                                    '' AS BingFaZhengQiTa,

                                    '' AS MaZuiWeiXing,
                                    '' AS ShiShui,
                                    '' AS YiShiMoHu,
                                    '' AS HunMi,
                                    '' AS YSZA_QiTa,

                                    '' AS YiYuZheng,
                                    '' AS JingShenFenLieZheng,
                                    '' AS LaoNianChiDaiZheng,
                                    '' AS JSZA_QiTa ,

                                    '' AS YingYouEr ,
                                    '' AS LaoNianRen ,
                                    '' AS NL_QiTa ,                                    

                                    '' AS JiaShuYiJian,
                                    '' AS QianMing,
                                    '' AS YuHuanZheGuanXi,
                                    '' AS HuShiQianMing,

                                    '' AS YuFang_DieDao ,
                                    '' AS YuFang_ZhuiChuang ,
                                    '' AS YuFang_ZiShang ,
                                    '' AS YuFang_shangren ,
                                    '' AS YuFang_zixingliyuan ,
                                    '' AS YuFang_ZiSha ,
                                    '' AS YuFang_Qita ,

                                    SUBSTRING(' ', 1, 10) AS RiQi 
                            FROM    dbo.NursingFormRecord(NOLOCK)
                            WHERE   FormId = {0} 
                                    AND patientid = '{1}' 
                                    AND VisitId = {2} {3} 
                            ORDER BY [AssessmentDate] DESC";

            string whereList = string.Empty;
            if (StatDate != "" && EndDate != "")
            {
                StatDate += " 00:00:00";
                EndDate += " 00:00:00";
                whereList = string.Format(" AND [AssessmentDate]>=CONVERT(DATETIME,'{0}') AND  [AssessmentDate]<CONVERT(DATETIME,'{1}')", StatDate, Convert.ToDateTime(EndDate).AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            select_sql = string.Format(select_sql, FormId, patientid, VisitId, whereList);
            DataTable dt = (SqlHelper.ExecuteDataset(select_sql, CommandType.Text)).Tables[0];
            if (dt.Rows.Count <= 0) return;
            DataTable dtt = dt.Clone();
            string theTrue = "√";
            foreach (DataRow dr in dt.Rows)
            {
                var NursingFormData = Convert.ToString(dr["NursingFormData"]);
                Knowing fd = new Knowing();
                fd = XmlUtil.XMLToObject<Knowing>(NursingFormData, Encoding.UTF8);
                DataRow newRow = dtt.NewRow();

                //约束工具 原因
                if (!string.IsNullOrEmpty(fd.YueShuGongJu.YuanYin))
                {
                    string ysgj_yy = fd.YueShuGongJu.YuanYin.IndexOf("#") < 0 ? "0#0#0#0" : fd.YueShuGongJu.YuanYin;
                    string[] yygj_yyArray = ysgj_yy.Split('#');
                    if (yygj_yyArray.Length >= 4)
                    {
                        newRow["ZaoDong"] = yygj_yyArray[0].Equals("1") ? theTrue : string.Empty;
                        newRow["ShenZhiYiChang"] = yygj_yyArray[1].Equals("1") ? theTrue : string.Empty;
                        newRow["JingShenZhangAi"] = yygj_yyArray[2].Equals("1") ? theTrue : string.Empty;
                        newRow["ZhiTiZhiDong"] = yygj_yyArray[3].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["YuanYinQiTa"] = !string.IsNullOrEmpty(fd.YueShuGongJu.YY_QiTa) ? theTrue : string.Empty;
                //约束工具 预防
                if (!string.IsNullOrEmpty(fd.YueShuGongJu.YuFang))
                {
                    string ysgj_yf = fd.YueShuGongJu.YuFang.IndexOf("#") < 0 ? "0#0#0#0#0" : fd.YueShuGongJu.YuFang;
                    string[] ysgj_yf_arrary = ysgj_yf.Split('#');
                    if (ysgj_yf_arrary.Length >= 5)
                    {
                        newRow["BaGuan"] = ysgj_yf_arrary[0].Equals("1") ? theTrue : string.Empty;
                        newRow["ZiShang"] = ysgj_yf_arrary[1].Equals("1") ? theTrue : string.Empty;
                        newRow["ShangRen"] = ysgj_yf_arrary[2].Equals("1") ? theTrue : string.Empty;
                        newRow["ZhuiChuang"] = ysgj_yf_arrary[3].Equals("1") ? theTrue : string.Empty;
                        newRow["ZhiTiGuoDuHuoDong"] = ysgj_yf_arrary[4].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["YuFangQiTa"] = !string.IsNullOrEmpty(fd.YueShuGongJu.YF_QiTa) ? theTrue : string.Empty;
                //约束工具 并发症
                if (!string.IsNullOrEmpty(fd.YueShuGongJu.BingFaZheng))
                {
                    string ysgj_bfz = fd.YueShuGongJu.BingFaZheng.IndexOf("#") < 0 ? "0#0#0#0" : fd.YueShuGongJu.BingFaZheng;
                    string[] ysgj_bfz_array = ysgj_bfz.Split('#');
                    if (ysgj_bfz_array.Length >= 4)
                    {
                        newRow["PiFuHongZhong"] = ysgj_bfz_array[0].Equals("1") ? theTrue : string.Empty;
                        newRow["PoKui"] = ysgj_bfz_array[1].Equals("1") ? theTrue : string.Empty;
                        newRow["QiPao"] = ysgj_bfz_array[2].Equals("1") ? theTrue : string.Empty;
                        newRow["GanRan"] = ysgj_bfz_array[3].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["BingFaZhengQiTa"] = !string.IsNullOrEmpty(fd.YueShuGongJu.BFZ_QiTa) ? theTrue : string.Empty;
                //二十四小时看护 意识障碍
                if (!string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.YiShiZhangAi))
                {
                    string ershisi_ysza = fd.ErShiSiXiaoShiKanHu.YiShiZhangAi.IndexOf("#") < 0 ? "0#0#0#0" : fd.ErShiSiXiaoShiKanHu.YiShiZhangAi;
                    string[] ershisi_ysza_Array = ershisi_ysza.Split('#');
                    if (ershisi_ysza_Array.Length >= 4)
                    {
                        newRow["MaZuiWeiXing"] = ershisi_ysza_Array[0].Equals("1") ? theTrue : string.Empty;
                        newRow["ShiShui"] = ershisi_ysza_Array[1].Equals("1") ? theTrue : string.Empty;
                        newRow["YiShiMoHu"] = ershisi_ysza_Array[2].Equals("1") ? theTrue : string.Empty;
                        newRow["HunMi"] = ershisi_ysza_Array[3].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["YSZA_QiTa"] = !string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.YSZA_QiTa) ? theTrue : string.Empty;
                //二十四小时看护 精神障碍
                if (!string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.JingShenZhangAi))
                {
                    string ershisi_jsza = fd.ErShiSiXiaoShiKanHu.JingShenZhangAi.IndexOf("#") < 0 ? "0#0#0" : fd.ErShiSiXiaoShiKanHu.JingShenZhangAi;
                    string[] ershisi_jsza_array = ershisi_jsza.Split('#');
                    if (ershisi_jsza_array.Length >= 3)
                    {
                        newRow["YiYuZheng"] = ershisi_jsza_array[0].Equals("1") ? theTrue : string.Empty;
                        newRow["JingShenFenLieZheng"] = ershisi_jsza_array[1].Equals("1") ? theTrue : string.Empty;
                        newRow["LaoNianChiDaiZheng"] = ershisi_jsza_array[2].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["JSZA_QiTa"] = !string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.JSZA_QiTa) ? theTrue : string.Empty;
                //二十四小时看护 年龄
                if (!string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.NianLin))
                {
                    string ershisi_nl = fd.ErShiSiXiaoShiKanHu.NianLin.IndexOf("#") < 0 ? "0#0" : fd.ErShiSiXiaoShiKanHu.NianLin;
                    string[] ershisi_nl_array = ershisi_nl.Split('#');
                    if (ershisi_nl_array.Length >= 2)
                    {
                        newRow["YingYouEr"] = ershisi_nl_array[0].Equals("1") ? theTrue : string.Empty;
                        newRow["LaoNianRen"] = ershisi_nl_array[1].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["NL_QiTa"] = !string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.NL_QiTa) ? theTrue : string.Empty;
                newRow["JiaShuYiJian"] = fd.JiaShuYiJian;
                newRow["QianMing"] = fd.QianMing;
                newRow["YuHuanZheGuanXi"] = fd.YuHuanZheGuanXi;
                newRow["HuShiQianMing"] = fd.HuShiQianMing;
                //二十四小时看护 预防
                if (!string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.YuFang))
                {
                    string ershisi_yufang = fd.ErShiSiXiaoShiKanHu.YuFang.IndexOf("#") < 0 ? "0#0#0#0#0#0" : fd.ErShiSiXiaoShiKanHu.YuFang;
                    string[] ershisi_yufang_array = ershisi_yufang.Split('#');
                    if (ershisi_yufang_array.Length >= 6)
                    {
                        newRow["YuFang_DieDao"] = ershisi_yufang_array[0].Equals("1") ? theTrue : string.Empty;
                        newRow["YuFang_ZhuiChuang"] = ershisi_yufang_array[1].Equals("1") ? theTrue : string.Empty;
                        newRow["YuFang_ZiShang"] = ershisi_yufang_array[2].Equals("1") ? theTrue : string.Empty;
                        newRow["YuFang_shangren"] = ershisi_yufang_array[3].Equals("1") ? theTrue : string.Empty;
                        newRow["YuFang_zixingliyuan"] = ershisi_yufang_array[4].Equals("1") ? theTrue : string.Empty;
                        newRow["YuFang_ZiSha"] = ershisi_yufang_array[5].Equals("1") ? theTrue : string.Empty;
                    }
                }
                newRow["YuFang_Qita"] = !string.IsNullOrEmpty(fd.ErShiSiXiaoShiKanHu.YF_QiTa) ? theTrue : string.Empty;
                newRow["RiQi"] = string.IsNullOrEmpty(fd.RiQi) ? string.Empty : Convert.ToDateTime(fd.RiQi).ToString("yyyy-MM-dd");
                //ID隐藏，双击触发事件
                newRow["Id"] = dr["ID"];
                dtt.Rows.Add(newRow);
            }

            gridControl1.DataSource = dtt;
        }
        #endregion

        #region GridView设置
        //GridView设置
        private void SetBandGridView()
        {
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 查询
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            string StatDate = this.dateStart.Text;
            string EndDate = this.dateEnd.Text;

            if (string.IsNullOrEmpty(StatDate) || string.IsNullOrEmpty(EndDate))
            {
                MessageBox.Show("查询日期不能为空！");
            }
            LoadData(StatDate, EndDate);
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            AddAgreeBookNursing add = new AddAgreeBookNursing(LoginUser, Patientinfo);
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                LoadData("", "");
            }
        }
        #endregion

        #region 新增
        private void BtnCreate_Click(object sender, EventArgs e)
        {
            AddAgreeBookNursing addForm = new AddAgreeBookNursing(LoginUser, Patientinfo);
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                LoadData("", "");
            }
        }
        #endregion

        #region 修改
        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddAgreeBookNursing addForm = new AddAgreeBookNursing(LoginUser, Patientinfo);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    LoadData("", "");
                }
            }
        }
        #endregion

        #region 打印
        private void BtnPrint_Click(object sender, EventArgs e)
        {
            //单张打印
            //AgreeBookPrint f = new AgreeBookPrint(patientId, Convert.ToInt32(visit), (int)FormType.Knowing);
            //多张打印
            AgreeBookPrints f = new AgreeBookPrints(LoginUser, Patientinfo, string.Empty);
            f.Show();
        }
        #endregion

        #region 删除
        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = this.dateStart.Text;
                    string dateEnd = this.dateEnd.Text;
                    LoadData(dateStart, dateEnd);
                }
            }
        }
        #endregion
    }
}
